package project3;

/**
 * @author oldliu
 * @since 1.0
 */
public class Disk {
    private String label;
    private long size;//M
    private int count=0;//软件个数
    private Soft[] softs=new Soft[0];
    public Disk(){

    }

    public Disk(String label, long size) {
        this.label = label;
        this.size = size;
    }
    //安装到本磁盘
    public boolean install(Soft soft){
        if(getLeftSpace()<soft.getSize())
            return false;//安装失败
        if(count>=this.softs.length){
            Soft[] s2=new Soft[(int) (this.softs.length*1.5+1)];
            for(int i=0;i<this.softs.length;i++){
                s2[i]=softs[i];
            }
            this.softs=s2;
        }
        this.softs[count++]=soft;
        return true;
    }
    //获得磁盘空间大小
    public long getLeftSpace(){
        long temp=this.size;
        for(Soft s:this.softs){
            if(s==null)
                return temp;
            temp-=s.getSize();
        }
        return temp;
    }

    public void show(){
        System.out.println(this.label+"盘，空间是"+this.size+"M");
        System.out.println("安装有"+this.count+"个软件");
        for(int i=0;i<count;i++)
            this.softs[i].show();
    }

    public String getLabel() {
        return label;
    }

    public long getSize() {
        return size;
    }
}
